(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.process.processConfig.deleteRejectController', deleteRejectController);
	deleteRejectController.$inject = [ '$scope', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'processConfigService' ];
	function deleteRejectController($scope, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, processConfigService) {
		var ctrl = this, processConfigInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;

		ctrl.init = function() {
			var params = {
					taskId : processConfigInfo.taskId
			};
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.variablesCfg.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.processConfig.variables';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			authService.deleteReject(params).then(function(response) {
				ctrl.processConfig = response.data.model;
				ctrl.processConfig.taskId = response.data.task.taskId || processConfigInfo.taskId;
			});
		};
		
		ctrl.deleteRejectSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.processConfig.process.wfprocName]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteRejectSubmit(ctrl.processConfig).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};
		
		ctrl.deleteRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.processConfig.process.wfprocName]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteRejectCancel(ctrl.processConfig).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.process.list',
					'state' : 'cda.process.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.process.list',
					'state' : 'cda.process.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);